package cn.itcast.oa.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.base.ModelDrivenBaseAction;
import cn.itcast.oa.model.Forum;
import cn.itcast.oa.model.PageBean;
import cn.itcast.oa.model.Reply;
import cn.itcast.oa.model.Topic;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class TopicAction extends ModelDrivenBaseAction<Topic>{
	  
	private Long forumId;
	
	 public String show() throws Exception{
		 Topic topic=topicService.getById(model.getId());
		 ActionContext.getContext().put("topic", topic);
		 
//		 List<Reply> replyList=replyService.findByTopic(topic);
//		 ActionContext.getContext().put("replyList", replyList);
		 PageBean pageBean=replyService.getPageBean(pageNum,topic);
		 ActionContext.getContext().getValueStack().push(pageBean);
		return "show";
	}
	 
	 
     public String add() throws Exception{	
    		model.setForum(forumService.getById(forumId));

    		// >> 当前可以直接获取的信息
    		model.setAuthor(getCurrentUser()); // 作者，当前登录的用户
    		model.setIpAddr(ServletActionContext.getRequest().getRemoteAddr()); // IP地址，当前请求中的IP信息
    		model.setPostTime(new Date()); // 发表
    		topicService.save(model);
    	    return "toShow";
     }
     
     public String addUI() throws Exception{ 
    	 Forum forum=forumService.getById(forumId);
    	 ActionContext.getContext().put("forum",forum);
    	 return "addUI";
     }
     
     
     //得到数据
	public Long getForumId() {
		return forumId;
	}
	public void setForumId(Long forumId) {
		this.forumId = forumId;
	}

}
